﻿using System;
using System.Linq;
using StudentInfo.Security.Enums;

namespace StudentInfo.Security.Services
{
    public class RolePermissionService
    {
        public static bool HasAccessByUser(Guid id, PermissionType required, CrudType crud = CrudType.CanRead)
        {
            Permission permission = PermissionService.GetByName(required.ToString());
            var roles = RoleService.GetByUser(id);

            return roles.Any(r => r.RolePermissions.Any(rp => rp.PermissionId == permission.Id && HasAccess(rp, crud)));
        }

        public static bool HasAccess(RolePermission rp, CrudType crudType)
        {
            return rp.CrudType.HasFlag(crudType);
        }
    }
}